package UILayer;

import java.util.Scanner;

import CtrLayer.KundeCtr;
import ModelLayer.Kunde;

public class KundeUI {
	private KundeCtr kCtr;

	/**
	 * Constructor for the KundeUI that instances the KundeCtr class
	 * */
	public KundeUI() {
		try {
			kCtr = new KundeCtr();
		} catch (Exception e) {
			System.out.println("cant instanciate kundeCtr");
		}
	}

	public void startKundeUI() {
		boolean start = false;
		while (!start) {
			Scanner input = new Scanner(System.in);
			System.out.println("1. Create new Kunde");
			System.out.println("2. Find Kunde");
			System.out.println("3. Edit Kunde");
			// System.out.println("");
			// System.out.println("");
			int choice = input.nextInt();

			switch (choice) {
			case 1:
				createNewKunde();
				break;
			case 2:
				findKunde();
				break;
			case 3:
				editKunde();
				break;
			default:
				start = true;
				break;

			}
		}
	}

	/**
	 * Create new kunde method Asks for inputs of the costumer.
	 * */
	/**
	 * Create new kunde method Asks for inputs of the costumer.
	 * */
	public void createNewKunde() {
		Scanner input = new Scanner(System.in);

		System.out.println("Phone: ");
		String phone = input.next();
		input.nextLine();
		Kunde k = kCtr.findKunde(phone);

		if (k != null) {
			System.out.println("A customer already exits with that phone number!");
		} else {
			System.out.println("Name: ");
			String name = input.next();
			input.nextLine();
			System.out.println("Account: ");
			String acc = input.next();
			input.nextLine();
			System.out.println("Address: ");
			String add = input.next();
			input.nextLine();
			System.out.println("Costumer discount(In percent): ");
			int dis = input.nextInt();
			input.nextLine();

			System.out.println("E-mail: ");
			String email = input.next();

			boolean start = checkMail(email);
			while (!start) {
				System.out.println("Email is not valid enter a new one!");
				System.out.println("E-mail: ");
				email = input.next();
				start = checkMail(email);
			}

			/**
			 * Create new Kunde object and add it to the container.
			 * 
			 * @param String
			 *            navn, String konto, String adresse, String tlf, int
			 *            kunderabat, String email
			 * */
			kCtr.createNewKunde(name, acc, add, phone, dis, email);
			System.out.println("Costumer added to system!");
		}
	}

	/**
	 * Find kunde Finds a kunde in system.
	 * */
	public void findKunde() {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter tlf: ");
		String tlf = input.next();
		Kunde k = kCtr.findKunde(tlf);
		if(k != null)
		{
			System.out.println("Name: " + k.getNavn());
			System.out.println("Phone: " + k.getTlf());
			System.out.println("Address: " + k.getAddresse());
			System.out.println("Email: " + k.getEmail());
		}
		else
		{
			System.out.println("No customers exits with that phone number!");
		}
	}

	/**
	 * Edit kunde
	 * 
	 * */
	public void editKunde() {
		EditKundeUI ekui = new EditKundeUI();
		ekui.startNewEditKundeUI();
	}

	public boolean checkMail(String mail) {
		String[] mailCombination = { ".dk", ".org", ".com", ".biz", ".eu",
				".me", ".as", ".cc", ".de", ".email" };
		boolean avaiable = false;

		for (String mailcombi : mailCombination) {
			if (mail.endsWith(mailcombi) && mail.contains("@")) {
				avaiable = true;
			}
		}

		return avaiable;
	}

}
